package com.youhome.analysis.writer;

import com.youhome.analysis.model.CustomerType;
import com.youhome.service.listhub.ListingItemType;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by chen365 on 2018/04/26.
 */
@Service
public class HouseItemWriter implements ItemWriter<ListingItemType> {

    private static final String INSERT_SQL_CUSTOMER = "insert into customer" +
            "(id, gender, firstname, lastname, phoneNumber)" +
            " values(?, ?, ?, ?, ?)";

    private static final String INSERT_SQL_CUSTOMER_BILLING_ADDRESS = "insert into customer_billing_address" +
            "(id,postalCode,receiver,addressLine1,addressLine2)" +
            " values(?, ?, ?, ?, ?)";

    private static final String INSERT_SQL_CUSTOMER_PRIMARY_ADDRESS = "insert into customer_primary_address" +
            "(id,postalCode,addressLine1,addressLine2)" +
            " values(?, ?, ?, ?)";

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void write(List<? extends ListingItemType> list) throws Exception {
        for (ListingItemType item : list) {
            System.err.println(item.toString());

            /*
            jdbcTemplate.update(
                    INSERT_SQL_CUSTOMER,
                    item.getId(),
                    item.getGender(),
                    item.getFirstname(),
                    item.getLastname(),
                    item.getPhoneNumber()
            );

            jdbcTemplate.update(
                    INSERT_SQL_CUSTOMER_BILLING_ADDRESS,
                    item.getId(),
                    item.getAddress().getBillingAddress().getPostalCode(),
                    item.getAddress().getBillingAddress().getReceiver(),
                    item.getAddress().getBillingAddress().getAddressLine1(),
                    item.getAddress().getBillingAddress().getAddressLine2()
            );

            jdbcTemplate.update(
                    INSERT_SQL_CUSTOMER_PRIMARY_ADDRESS,
                    item.getId(),
                    item.getAddress().getBillingAddress().getPostalCode(),
                    item.getAddress().getBillingAddress().getAddressLine1(),
                    item.getAddress().getBillingAddress().getAddressLine2()
            );

            */
        }
    }


}
